import BaseController from '../baseController';
export default class LoginController extends BaseController {
  public async login() {
    const { ctx } = this;
    const { userName, passWord, captcha } = ctx.request.body;
    const cap: string = await ctx.getRedis(ctx.request.ip);
    console.log('[ cap ]', cap);
    if (!captcha || !cap || captcha.toLowerCase() !== cap.toLowerCase()) {
      return ctx.error('验证码错误');
    }
    const loginRes = await ctx.service.system.login.login(userName, passWord);
    if (loginRes) {
      const userInfo = loginRes.findRes.dataValues;
      userInfo.password = '';
      ctx.success({
        msg: '登陆成功',
        data: {
          token: loginRes.token,
          ...userInfo,
        },
      });
    } else {
      ctx.error('登陆失败');
    }
  }

  // captcha 登陆验证码
  public async captcha() {
    const { ctx } = this;
    const captcha = await ctx.service.system.login.captcha();
    ctx.response.type = 'image/svg+xml';
    ctx.body = captcha.data;
  }
}
